<%--
  Created by IntelliJ IDEA.
  User: 沐白
  Date: 2020/8/20
  Time: 15:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>商品采购</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/laydate/need/laydate.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/toastr/toastr.min.css"/>
    <script src="${pageContext.request.contextPath}/static/js/jquery-2.1.0.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/static/laydate/laydate.js"></script>
    <script src="${pageContext.request.contextPath}/static/toastr/toastr.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/Myjs.js"></script>
    <style type="text/css">
        .table-bordered > tbody > tr > td {
            vertical-align: middle;
        }
    </style>
</head>

<body>
<!--全局盒子结束 -->

<h3 class="text-center">采购信息</h3>
<div class="form-group" style="margin-top: 10px">
    <!--全局盒子 -->
    <div class="form-group">
        <!--顶部盒子 -->
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <label class="input-group-addon">采购单编号</label>
                        <input type="text" class="form-control" id="storageId" placeholder="采购单编号"/>
                    </div>
                </td>
                <td>
                    <div class="input-group">
                        <label class="input-group-addon">经办人</label>
                        <input type="text" class="form-control" id="staffname" placeholder="经办人"/>
                    </div>
                </td>
                <td>
                    <div class="input-group">
                        <label class="input-group-addon">申请时间</label>
                        <input type="text" class="form-control" id="sqshijian" placeholder="申请时间"/>
                    </div>
                </td>
                <td>
                    <input type="button" class="btn btn-primary" id="shoubtn" value="搜索"/>
                </td>
            </tr>
        </table>
    </div>
    <!--顶部盒子结束 -->
    <div class="form-group">
        <!--中部盒子 -->
        <table class="table table-striped table-bordered text-center table-hover">
            <tr class="info">
                <td>订单编号</td>
                <td>经办人</td>
                <td>时间</td>
                <td>总价</td>
                <td>状态</td>
                <td>操作</td>
            </tr>
            <tbody id="tb"></tbody>
        </table>
    </div>
    <!--中部盒子结束 -->
    <div class="form-inline">
        <!--下部盒子 -->
        <%--下边的文字下拉菜单还有按钮 --%>
        <div class="form-inline">
            <label>显示第 <span id="rowstart"></span> 到第 <span id="rowend"></span> 条记录,总共<span id="rowCount"></span>条记录
                每页显示
                <select class="form-control" id="pageSize">
                    <option value="5">5</option>
                    <option value="10">10</option>
                    <option value="20">20</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select>
                条记录</label>
            <label class="btn-group pull-right">
                <a href="javascript:search(pageBean.pageNo-1)" class="btn btn-default"><</a>
                <div id="btns" class="btn-group"></div>
                <a href="javascript:search(pageBean.pageNo+1)" class="btn btn-default">></a>
            </label>
        </div>
    </div>
    <!--下部盒子结束 -->
</div>
<div class="modal" id="ddinfo">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <!--模态框头部 -->
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">采购单信息</h4>
            </div>
            <div class="modal-body">
                <!--模态框内容 -->
                <div class="row">
                    <div class="form-group col-sm-6">
                        <div class="input-group  col-sm-12">
                            <div class="input-group-addon">
                                采购单编号
                            </div>
                            <input type="text" class="form-control" disabled placeholder="采购单编号"/>
                        </div>
                    </div>
                    <div class="form-group col-sm-6">
                        <div class="input-group  col-sm-12">
                            <div class="input-group-addon">
                                采购总价
                            </div>
                            <input type="text" class="form-control" disabled placeholder="采购总价"/>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-sm-6">
                        <div class="input-group  col-sm-12">
                            <div class="input-group-addon">
                                采购时间
                            </div>
                            <input type="text" class="form-control" disabled placeholder="采购时间"/>
                        </div>
                    </div>
                    <div class="form-group col-sm-6">
                        <div class="input-group  col-sm-12">
                            <div class="input-group-addon">
                                采购单状态
                            </div>
                            <input type="text" class="form-control" name="status" disabled placeholder="采购单状态"/>
                        </div>
                    </div>
                </div>
                <table class="table table-hover table-bordered table-striped text-center">
                    <tr class="info">
                        <td>商品名称</td>
                        <td>商品数量</td>
                        <td>商品进价</td>
                        <td>商品备注</td>
                    </tr>
                    <tbody id="cgdxx"></tbody>
                </table>
            </div>
            <div class="modal-footer">
                <!--模态框下边的按钮 -->
                <input type="button" class="btn btn-default" data-dismiss="modal" value="关闭"/>
                <input type="button" class="btn btn-success" onclick="ruku()" value="入库"/>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    let pageBean = new Object();
    pageBean.pageNo = 1;
    pageBean.pageSize = 5;
    pageBean.factor = [];
    $(function () {
        search(1);
        //点击搜索按钮查询
        $('#shoubtn').click(function () {
            search(1);
        });
        $('#pageSize').on('change', function () {
            pageBean.pageSize = parseInt($('#pageSize').val());
            search(1);
        });

        $('#ddinfo').on('hide.bs.modal', function () {
            $(this).clea();
        });

    });

    function ruku() {
        let flag = event.target.parentNode.parentNode.getElementsByTagName("input")[3].value;
        if (flag == "待入库") {
            let storageId = $('#ddinfo input').get(0).value;
            let goodsName = "";
            let goodsCount = "";
            let checker = $('#cgdxx tr');
            for (let i = 0; i < checker.length; i++) {
                let tds = checker[i].getElementsByTagName("td");
                if (i == checker.length - 1) {
                    goodsName += tds[0].innerHTML;
                    goodsCount += tds[1].innerHTML;
                } else {
                    goodsName += tds[0].innerHTML + ",";
                    goodsCount += tds[1].innerHTML + ",";
                }
            }
            rukuajax(storageId, goodsName, goodsCount);
        } else if (flag == "已入库") {
            toastr.warning("此采购单已入库");
        } else {
            toastr.warning("此采购单未审核");
        }
        $('#ddinfo').modal('hide');
        search(pageBean.pageNo);
    }

    function rukuajax(storageId, goodsName, goodsCount) {
        $.ajax({
            url: "/procurementController/ruku",
            data: {storageId: storageId, goodsName: goodsName, goodsCount: goodsCount},
            type: "post",
            async: false,
            success: function (data) {
                if (data > 0) {
                    toastr.success("入库成功")
                } else {
                    toastr.error("入库失败");
                }
            },
            dataType: "json"
        })
    }

    function infos() {
        let storageId = event.target.parentNode.parentNode.firstElementChild.innerHTML;
        $.ajax({
            url: "/procurementController/showInfo",
            type: "post",
            data: {storageId: storageId},
            success: function (data) {
                let tr = "";
                let storage = data;
                let inps = $('#ddinfo input');
                inps[0].value = storage['storageId'];
                inps[1].value = storage['price'];
                inps[2].value = storage['createTime'];
                let status = "";
                if (storage['status'] == 1) {
                    status = "待入库";
                } else if (storage['status'] == 2) {
                    status = "已入库";
                }
                inps[3].value = status;
                for (let i = 0; i < storage['storageinfoList'].length; i++) {
                    let momo=storage['storageinfoList'][i]['momo']==null?"":storage['storageinfoList'][i]['momo'];
                    tr += "<tr>" +
                        "<td>" + storage['storageinfoList'][i]['goods']['name'] + "</td>\n" +
                        "<td>" + storage['storageinfoList'][i]['goodsCount'] + "</td>\n" +
                        "<td>" + storage['storageinfoList'][i]['goodsPrice'] + "</td>\n" +
                        "<td>" + momo + "</td>" +
                        "</tr>";
                }
                $('#cgdxx').html(tr);
                $('#ddinfo').modal('show');
            },
            dataType: "json"
        })
        //判断上一页有没有

    }

    function search(num) {
        if (num > 0) {
            pageBean.pageNo = num;
        } else {
            pageBean.pageNo = 1;
            num = 1;
        }
        //判断下一页有没有
        if (num <= pageBean.totalPage) {
            pageBean.pageNo = num;
        } else {
            if (pageBean.totalPage != 0) {
                pageBean.pageNo = pageBean.totalPage;
            } else {
                pageBean.pageNo = 1;
            }
        }
        if ($("#storageId").val() != "") {
            if ($('#storageId').val().indexOf("'") != -1) {
                pageBean.factor.push(" 1=0 ");
            } else {
                pageBean.factor.push(" storageId like '%" + $("#storageId").val() + "%'");
            }
        }
        //经办人判断
        if ($("#staffname").val() != null && $("#staffname").val() != "") {
            $.ajax({
                url: "/newsController/findStaffIdByName",
                async: false,
                data: {name: $("#staffname").val()},
                success: function (data) {
                    let staffId = "";
                    if (data != "" && data != null) {
                        for (let i = 0; i < data.length; i++) {
                            if (i == data.length - 1) {
                                staffId += "" + data[i]["staffId"] + "";
                            } else {
                                staffId += "" + data[i]["staffId"] + ",";
                            }
                        }
                        pageBean.factor.push(" staffId in (" + staffId + ") ");
                    } else {
                        pageBean.factor.push(" 1=0 ");
                    }
                    show();
                },
                dataType: "json"
            });
        }
        //申请时间判断
        if ($("#sqshijian").val() != "") {
            if ($('#sqshijian').val().indexOf("'") != -1) {
                pageBean.factor.push(" 1=0 ");
            } else {
                pageBean.factor.push(" createTime >= '" + $("#sqshijian").val() + "'");
            }
        }
        show();
    }

    function show() {
        pageBean.data = "";
        $.post(
            "${pageContext.request.contextPath}/procurementController/show",
            pageBean,
            function (date) {
                pageBean = date;
                pageBean.factor = [];
                $('#rowCount').html(pageBean.rowCount);
                $('#rowstart').html(pageBean.rowCount == 0 ? 0 : pageBean.startIndex + 1);
                if (date['staff'] != null) {
                    $('#xzstaff').val(date['staff']['staffName']);
                }
                let con = pageBean.rowCount == 0 ? 0 :
                    pageBean.pageNo == pageBean.totalPage ?
                        pageBean.rowCount % pageBean.pageSize == 0 ?
                            pageBean.pageSize : pageBean.rowCount % pageBean.pageSize
                        : pageBean.pageSize;
                $('#rowend').html(pageBean.startIndex + con);
                let btns = "";
                for (let i = 0; i < pageBean.totalPage; i++) {
                    if (pageBean.pageNo == i + 1) {
                        btns += "<a href='javascript:search(" + (i + 1) + ");' class='btn btn-primary'>" + (i + 1) + "</a>";
                    } else {
                        btns += "<a href='javascript:search(" + (i + 1) + ");' class='btn btn-default'>" + (i + 1) + "</a>";
                    }
                }
                $('#btns').html(btns);
                //表格显示
                let tr = "";
                let storages = pageBean.data;
                if (storages != null) {
                    for (let i = 0; i < storages.length; i++) {
                        let status = "";
                        if (storages[i]['status'] == 1) {
                            status = "<input type='button' class='btn btn-xs btn-danger' value='待入库' >";
                        } else if (storages[i]['status'] == 2) {
                            status = "<input type='button' class='btn btn-xs btn-info' value='已入库' >";
                        }
                        let staffName = "";
                        if(storages[i]['staff']!=null){
                            staffName=storages[i]['staff']['staffName'];
                        }
                        tr += "<tr>" +
                            "<td>" + storages[i]['storageId'] + "</td>" +
                            "<td>" + staffName + "</td>" +
                            "<td>" + storages[i]['createTime'] + "</td>" +
                            "<td>" + parseFloat(storages[i]['price']).toFixed(2) + "</td>" +
                            "<td>" + status + "</td>" +
                            "<td><input type='button' class='btn btn-info' onclick='infos()' value='详情'></td>" +
                            "</tr>"
                    }
                }
                $('#tb').html(tr);
            },
            "json"
        );
    }

    //申请时间
    laydate.render({
        elem: '#sqshijian'
    });

</script>
</html>
